<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Examples on using the ogg:// wrapper.</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="oggvorbis.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="book.openal.html">OpenAL</a></div>
 <div class="up"><a href="oggvorbis.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="oggvorbis.examples-basisc" class="section">
  <h2 class="title">Examples on using the ogg:// wrapper.</h2>
  <p class="para">
   <div class="example" id="example-575">
    <p><strong>Example #1 Reading an OGG/Vorbis file</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />dl</span><span style="color: #007700">(</span><span style="color: #DD0000">"oggvorbis.so"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;By&nbsp;default,&nbsp;ogg://&nbsp;will&nbsp;decode&nbsp;to&nbsp;Signed&nbsp;16-bit&nbsp;Little&nbsp;Endian&nbsp;*/<br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'ogg://myaudio.ogg'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Collect&nbsp;some&nbsp;information&nbsp;about&nbsp;the&nbsp;file.&nbsp;*/<br /></span><span style="color: #0000BB">$metadata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_get_meta_data</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Inspect&nbsp;the&nbsp;first&nbsp;song&nbsp;(usually&nbsp;the&nbsp;only&nbsp;song,&nbsp;<br />&nbsp;&nbsp;&nbsp;but&nbsp;OGG/Vorbis&nbsp;files&nbsp;may&nbsp;be&nbsp;chained)&nbsp;*/<br /></span><span style="color: #0000BB">$songdata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$metadata</span><span style="color: #007700">[</span><span style="color: #DD0000">'wrapper_data'</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">];<br /><br />echo&nbsp;</span><span style="color: #DD0000">"OGG/Vorbis&nbsp;file&nbsp;encoded&nbsp;by:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$songdata</span><span style="color: #007700">[</span><span style="color: #DD0000">'vendor'</span><span style="color: #007700">]}</span><span style="color: #DD0000">\n."</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"&nbsp;&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$songdata</span><span style="color: #007700">[</span><span style="color: #DD0000">'channels'</span><span style="color: #007700">]}</span><span style="color: #DD0000">&nbsp;channels&nbsp;of&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$songdata</span><span style="color: #007700">[</span><span style="color: #DD0000">'rate'</span><span style="color: #007700">]}</span><span style="color: #DD0000">Hz&nbsp;sampling&nbsp;encoded&nbsp;at&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$songdata</span><span style="color: #007700">[</span><span style="color: #DD0000">'bitrate_nominal'</span><span style="color: #007700">]}</span><span style="color: #DD0000">bps.\n"</span><span style="color: #007700">;<br />foreach(</span><span style="color: #0000BB">$songdata</span><span style="color: #007700">[</span><span style="color: #DD0000">'comments'</span><span style="color: #007700">]&nbsp;as&nbsp;</span><span style="color: #0000BB">$comment</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;&nbsp;</span><span style="color: #0000BB">$comment</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /><br />while&nbsp;(</span><span style="color: #0000BB">$audio_data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">8192</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Do&nbsp;something&nbsp;with&nbsp;the&nbsp;PCM&nbsp;audio&nbsp;we're&nbsp;extracting&nbsp;from&nbsp;the&nbsp;OGG.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Copying&nbsp;to&nbsp;/dev/dsp&nbsp;is&nbsp;a&nbsp;good&nbsp;target&nbsp;on&nbsp;linux&nbsp;systems,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just&nbsp;remember&nbsp;to&nbsp;setup&nbsp;the&nbsp;device&nbsp;for&nbsp;your&nbsp;sampling&nbsp;mode&nbsp;first.&nbsp;*/<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>

  <p class="para">
   <div class="example" id="example-576">
    <p><strong>Example #2 Encode an audio file to OGG/Vorbis</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />dl</span><span style="color: #007700">(</span><span style="color: #DD0000">'oggvorbis.so'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$context&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_context_create</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'ogg'</span><span style="color: #007700">=&gt;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'pcm_mode'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">OGGVORBIS_PCM_S8</span><span style="color: #007700">,&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Signed&nbsp;8bit&nbsp;audio&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'rate'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">44100</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;44kHz&nbsp;CD&nbsp;quality&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'bitrate'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0.5</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Midquality&nbsp;VBR&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'channels'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Mono&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'serialno'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">12345</span><span style="color: #007700">)));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Unique&nbsp;within&nbsp;our&nbsp;stream&nbsp;*/<br /><br />/*&nbsp;Open&nbsp;file&nbsp;for&nbsp;appending.&nbsp;&nbsp;This&nbsp;will&nbsp;"chain"&nbsp;a&nbsp;second&nbsp;OGG&nbsp;stream&nbsp;at&nbsp;the&nbsp;end&nbsp;of&nbsp;the&nbsp;first.&nbsp;*/<br /></span><span style="color: #0000BB">$ogg&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'ogg://mysong.ogg'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'a'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$context</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$pcm&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'mysample.pcm'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Compress&nbsp;the&nbsp;raw&nbsp;PCM&nbsp;audio&nbsp;from&nbsp;mysample.pcm&nbsp;into&nbsp;mysong.ogg&nbsp;*/<br /></span><span style="color: #0000BB">stream_copy_to_stream</span><span style="color: #007700">(</span><span style="color: #0000BB">$pcm</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$ogg</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$pcm</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$ogg</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="oggvorbis.examples.html">范例</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="book.openal.html">OpenAL</a></div>
 <div class="up"><a href="oggvorbis.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
